﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using System.Windows.Controls;
using System.Windows.Input;
using DevExpress.AgDataGrid.Internal;
using System.Windows.Controls.Primitives;
using DevExpress.AgDataGrid.Data;
using System.Windows;
using System.Linq;
using System;
using System.Reflection;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Windows.Media;
using System.Windows.Browser;
using DevExpress.AgDataGrid;
namespace DevExpress.AgDataGrid {
	public interface IAgDataGridTotalSelectionOwner {
		void EndTotalSelection(AgDataGridTotalSelection totalSelection);
	}
	public class AgDataGridTotalSelection : Control {
		IAgDataGridTotalSelectionOwner owner;
		List<SummaryItemType> totals;
		List<SummaryItemType> unavailable;
		public AgDataGridTotalSelection(IAgDataGridTotalSelectionOwner owner, List<SummaryItemType> totals, List<SummaryItemType> unavailable) {
			DefaultStyleKey = typeof(AgDataGridTotalSelection);
			this.owner = owner;
			this.totals = totals;
			this.unavailable = unavailable;
			LostFocus += new RoutedEventHandler(AgDataGridTotalSelection_LostFocus);
			((UIElement)Application.Current.RootVisual).MouseLeftButtonDown += new MouseButtonEventHandler(Root_MouseLeftButtonDown);
		}
		void Root_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
			Close();
		}
		protected bool FindParent(FrameworkElement o) {
			if(o == null) return false;
			if(VisualTreeHelper.GetParent(o) == null) return false;
			if(o is AgDataGridTotalSelection) return true;
			if(VisualTreeHelper.GetParent(o) is AgDataGridTotalSelection) return true;
			return FindParent(VisualTreeHelper.GetParent(o) as FrameworkElement);
		}
		void AgDataGridTotalSelection_LostFocus(object sender, RoutedEventArgs e) {
			if(!FindParent(FocusManager.GetFocusedElement() as FrameworkElement)) Close();
		}
		protected List<SummaryItemType> Totals { get { return totals; } }
		protected List<SummaryItemType> Unavailable { get { return unavailable; } }
		public override void OnApplyTemplate() {
			base.OnApplyTemplate();
			Panel checkPanel = (Panel)GetTemplateChild("CheckPanel");
			Style checkBoxStyle = dxElementStyles.GetStyle("CheckBoxStyle");
			foreach (CheckBox item in checkPanel.Children) {
				if (item.Style == null) {
					item.Style = checkBoxStyle;
				}
			}
			((Button)GetTemplateChild("CloseButton")).Click += new RoutedEventHandler(AgDataGridTotalSelection_Click);
			foreach(SummaryItemType item in Unavailable) {
				((CheckBox)GetTemplateChild(item.ToString())).SetVisible(false);
			}
			foreach(SummaryItemType item in Totals) {
				CheckBox cb = ((CheckBox)GetTemplateChild(item.ToString()));
				cb.IsChecked = true;
			}
			((CheckBox)GetTemplateChild("Count")).Focus();
		}
		void AgDataGridTotalSelection_Click(object sender, RoutedEventArgs e) {
			Close();			
		}
		protected override Size ArrangeOverride(Size finalSize) {
			Size res = base.ArrangeOverride(finalSize);
			RenderTransform = new TranslateTransform() { Y = -DesiredSize.Height };
			return res;
		}
		public IAgDataGridTotalSelectionOwner Owner { get { return owner; } }
		protected void Close() {
			if(VisualTreeHelper.GetParent(this) == null) return;
			Owner.EndTotalSelection(this);
		}
		protected internal List<SummaryItemType> GetSummaryInfo() {
			List<SummaryItemType> res = new List<SummaryItemType>();
			foreach(FieldInfo field in typeof(SummaryItemType).GetFields().ToArray()) {
				if(field.FieldType == typeof(SummaryItemType)) {
					CheckBox cb = GetTemplateChild(field.Name) as CheckBox;
					if(cb != null && cb.IsChecked == true) res.Add((SummaryItemType)field.GetRawConstantValue());
				}
			}
			return res;
		}
	}
}
